package com.mdhw.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.mdhw.config.R;
import com.mdhw.system.service.QRCodeService;
import com.mdhw.system.service.WechatAppletService;
import com.mdhw.utils.AliYunOssResult;
import com.mdhw.utils.AliYunOssServer;
import com.mdhw.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class QRCodeServiceImpl implements QRCodeService {

    @Autowired
    private AliYunOssServer aliYunOssServer;
    @Autowired
    private WechatAppletService wechatAppletService;


    public static void main(String[] args) throws WriterException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        System.out.println(JSONObject.toJSONString(qrCodeWriter));
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        BitMatrix bitMatrix = qrCodeWriter.encode("2", BarcodeFormat.QR_CODE, 350, 350, hints);
        Path file = FileSystems.getDefault().getPath("qrcode.png");
    }


    /**
     * 永久二维码
     * @param text
     * @param width
     * @param height
     * @param yaoqingma
     */
    @Override
    public R<String> generateQRCodeImage(String text, int width, int height, String yaoqingma) {
        try {
            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
            BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height, hints);

            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            bufferedImage.createGraphics();

            Graphics2D graphics = (Graphics2D) bufferedImage.getGraphics();
            graphics.setColor(java.awt.Color.WHITE);
            graphics.fillRect(0, 0, width, height);
            graphics.setColor(java.awt.Color.BLACK);
            Date nowTime = new Date();
            for (int i = 0; i < width; i++) {
                for (int j = 0; j < height; j++) {
                    if (bitMatrix.get(i, j)) {
                        graphics.fillRect(i, j, 1, 1);
                    }
                }
            }
            InputStream is = convertBufferedImageToInputStream(bufferedImage, "PNG");
            AliYunOssResult upload = aliYunOssServer.upload(is, "jiazhuangQrCode/" + DateUtil.formatDate(nowTime, DateUtil.yyyy_mm_dd_hh_mm_ss) + "__" + yaoqingma + ".PNG");
            String url = upload.getUrl();
            return R.success(url);
        } catch ( Exception e) {
            e.printStackTrace();
            return R.error("上传失败");
        }
    }

    public static InputStream convertBufferedImageToInputStream(BufferedImage image, String formatName) {
        try {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ImageIO.write(image, formatName, outputStream);
            return new ByteArrayInputStream(outputStream.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
